home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_5488.txt < prev    next >
Text File  |  1990-04-17  |  11KB  |  321 lines

  1. -- card: 5488 from stack: in.3
  2. -- bmap block id: 0
  3. -- flags: 4000
  4. -- background id: 3241
  5. -- name: FileToClip
  6. ----- HyperTalk script -----
  7. on Install
  8.   get ChooseTargetStack()
  9.   InstallResource XCMD,FileToClip,it
  10. end Install
  11.  
  12.  
  13. -- part 1 (button)
  14. -- low flags: 00
  15. -- high flags: A003
  16. -- rect: left=79 top=300 right=322 bottom=179
  17. -- title width / last selected line: 0
  18. -- icon id / first selected line: 0 / 0
  19. -- text alignment: 1
  20. -- font id: 0
  21. -- text size: 12
  22. -- style flags: 0
  23. -- line height: 16
  24. -- part name: FileToClip
  25. ----- HyperTalk script -----
  26. on mouseUp
  27.   FileToClip
  28.   put the result
  29. end mouseUp
  30.  
  31.  
  32.  
  33. -- part 2 (button)
  34. -- low flags: 00
  35. -- high flags: A003
  36. -- rect: left=299 top=300 right=322 bottom=438
  37. -- title width / last selected line: 0
  38. -- icon id / first selected line: 0 / 0
  39. -- text alignment: 1
  40. -- font id: 0
  41. -- text size: 12
  42. -- style flags: 0
  43. -- line height: 16
  44. -- part name: Show Pascal Source
  45. ----- HyperTalk script -----
  46. on mouseUp
  47.   set the visible of card field 1 to not the visible of card field 1
  48.   if the visible of card field 1 is true then
  49.     set the name of me to "Hide Pascal Source"
  50.   else set the name of me to "Show Pascal Source"
  51. end mouseUp
  52.  
  53.  
  54.  
  55. -- part 3 (field)
  56. -- low flags: 81
  57. -- high flags: 2007
  58. -- rect: left=12 top=26 right=298 bottom=491
  59. -- title width / last selected line: 0
  60. -- icon id / first selected line: 0 / 0
  61. -- text alignment: 0
  62. -- font id: 22
  63. -- text size: 10
  64. -- style flags: 0
  65. -- line height: 13
  66. -- part name: Source
  67.  
  68.  
  69. -- part contents for background part 16
  70. ----- text -----
  71. FILETOCLIP XCMD version 1.5
  72. Kevin Calhoun
  73.  
  74. The FileToClip XCMD copies the contents of a text file to the clipboard.  You choose the text file from a standard file dialog box.  Once the text is on the clipboard, you can then paste it into a field, into another application running concurrently under MultiFinder, or into a desk accessory (such as McSink).
  75.  
  76. If an error occurs, FileToClip will return an error message as the result.  Word 1 of this message will be "Error".  If the text was copied successfully, FileToClip returns the full pathname of the file as the result.
  77.  
  78. If you want the text to go directly to a HyperCard field without having to paste it manually, use the FileToField XCMD.
  79.  
  80. FileToClip takes no parameters.
  81.  
  82.  
  83. -- part contents for card part 3
  84. ----- text -----
  85. UNIT ReadToClipUnit;
  86.  
  87. { FileToClip XCMD ┬⌐ 1988-1989 by the Trustees of Dartmouth College }
  88. { Written by Kevin Calhoun }
  89.  
  90. { This source compatible with MPW Pascal 3.0 }
  91.  
  92. (*
  93. Pascal FileToClip.p
  94. Link -m ENTRYPOINT Γêé
  95.      -o "YourFile" Γêé
  96.      -rt XCMD=2234 Γêé
  97.      -sn Main=FileToClip Γêé
  98.      FileToClip.p.o Γêé
  99.     "{Libraries}"interface.o Γêé
  100.     "{PLibraries}"Paslib.o Γêé
  101.     "{Libraries}"HyperXLib.o
  102. *)
  103.  
  104. {$R-}
  105.  
  106. INTERFACE
  107.  
  108.   USES
  109.     Types,
  110.     Memory,
  111.     Files,
  112.     Resources,
  113.     Scrap,
  114.     Packages,
  115.     HyperXCmd;
  116.  
  117.   PROCEDURE EntryPoint (paramPtr : XCmdPtr);
  118.  
  119. IMPLEMENTATION
  120.  
  121.   PROCEDURE ReadFileToClip (paramPtr : XCmdPtr); FORWARD;
  122.   
  123.   PROCEDURE EntryPoint (paramPtr : XCmdPtr);
  124.   BEGIN
  125.     ReadFileToClip(paramPtr);
  126.   END;
  127.  
  128.   FUNCTION GetScreenBitsBounds: Rect;
  129.   { get screenbits.bounds from the QuickDraw globals }
  130.   TYPE
  131.     LongwordPtr = ^LONGINT;
  132.     BitMapPtr = ^BitMap;
  133.   CONST
  134.     screenBitsOffset = -122;
  135.     CurrentA5 = $904;
  136.   VAR
  137.     screenBitsPtr : BitMapPtr;
  138.     myLongwordPtr : LongwordPtr;
  139.   BEGIN
  140.     myLongwordPtr := LongwordPtr(CurrentA5);
  141.       { myLongwordPtr now points to the pointer to the first QD global }
  142.     myLongwordPtr := LongwordPtr(myLongwordPtr^);
  143.       { myLongwordPtr now points to the first QD global }
  144.     screenBitsPtr := BitMapPtr(myLongwordPtr^ + screenBitsOffset);
  145.       { screenBitsPtr now points to the screenBits BitMap }
  146.     GetScreenBitsBounds := screenBitsPtr^.bounds;
  147.   END;
  148.  
  149.   FUNCTION BuildThePathname (fName : Str255;
  150.                   vRefNum : INTEGER) : Str255;
  151. { Given the "short name" and vRefNum of a file, returns the full pathname. }
  152. { This function is adapted from Steve Maller's FileName XFCN published in }
  153. { HyperTalk Programming by Dan Shafer, Howard W. Sams & Company, 1988, }
  154. { pp. 399-403. }
  155.     VAR
  156.       name, fullPathName : Str255;
  157.       err : INTEGER;
  158.       myWDPB : WDPBPtr;
  159.       myCPB : CInfoPBPtr;
  160.       myPB : HParmBlkPtr;
  161.  
  162.   BEGIN
  163.     fullPathName := '';     { start with an empty pathname }
  164. { Allocate some memory in the heap for the parameter block. }
  165.     myCPB := CInfoPBPtr(NewPtr(SizeOf(HParamBlockRec)));
  166.     IF ord4(myCPB) > 0 THEN    { continue if mem allocation was OK }
  167.  
  168.       BEGIN
  169.         myWDPB := WDPBPtr(myCPB);
  170.         myPB := HParmBlkPtr(myCPB);
  171. { same pointer, different variations of the record -- see IM IV, p. 117 }
  172.         name := '';     { start with an empty name for the volume }
  173.  
  174.         WITH myPB^ DO
  175.           BEGIN
  176.             ioNamePtr := @name;   { we want the volume name }
  177.             ioCompletion := pointer(0);
  178.             ioVRefNum := vRefNum;  { returned by SFGetFile }
  179.             ioVolIndex := 0;  { use the vRefNum and name only to designate volume }
  180.           END;
  181.         err := PBHGetVInfo(myPB, FALSE);  { fill in the volume info }
  182.         IF err = noErr THEN
  183.  
  184.           BEGIN
  185. { Now we need the Working Directory (WD) information because we're }
  186. { going to step backwards from the file through all of the folders until }
  187. { we reach the root directory. }
  188.             WITH myWDPB^ DO
  189.               BEGIN
  190.                 ioVRefNum := vRefNum;  { this got set to 0 above }
  191.                 ioWDProcID := 0;   { use the vRefNum }
  192.                 ioWDIndex := 0;     { we want all directories }
  193.               END;
  194.             err := PBGetWDInfo(myWDPB, FALSE);
  195.             IF err = noErr THEN
  196.               BEGIN
  197.                 WITH myCPB^ DO
  198.                   BEGIN
  199.                     ioFDirIndex := -1;   { use the ioDirID field only }
  200.                     ioDrDirID := myWDPB^.ioWDDirID;   { info returned above }
  201.                   END;
  202.                 err := PBGetCatInfo(myCPB, FALSE);
  203.                 IF err = noErr THEN
  204.                   BEGIN
  205.  
  206. { Here starts the real work -- start to climb the tree by continually }
  207. { looking in the ioDrParID field for the next directory above until we fail... }
  208.                     myCPB^.ioDrDirID := myCPB^.ioDrParID;  { the first folder }
  209.                     fullPathName := CONCAT(myCPB^.ioNamePtr^, ':', fName);
  210.                     REPEAT
  211.                       myCPB^.ioDrDirID := myCPB^.ioDrParId;
  212.                       err := PBGetCatInfo(myCPB, FALSE);    { the next level }
  213. { Be careful of an error returned here -- it means the user chose a file on the }
  214. { desktop level of this volume.  If this is the case, just stop here and return }
  215. { "VolumeName:FileName"; otherwise loop until failure. }
  216.                       IF err = noErr THEN
  217.                         fullPathName := CONCAT(myCPB^.ioNamePtr^, ':', fullPathName);
  218.                     UNTIL err <> noErr;
  219.                   END;  { if PBGetCatInfo worked OK }
  220.               END;  { if PBGetWDInfo worked OK }
  221.           END;  { if PBHGetVInfo worked OK }
  222.         DisposPtr(pointer(myCPB));
  223.       END;  { if we had enough room for a new pointer }
  224.     BuildThePathname := fullPathName;
  225.   END;
  226.  
  227.   PROCEDURE ReadFileToClip (paramPtr : XCmdPtr);
  228.     VAR
  229.  
  230.       { for SFGetFile }
  231.       where : point;
  232.       typeList : SFTypeList;
  233.       reply : SFReply;
  234.       dlgt: DialogTHndl;
  235.       r: rect;
  236.       screen: rect;
  237.       h, v: INTEGER;
  238.  
  239.       theRefNum : INTEGER;  { file ref num for file manager calls }
  240.       err : OSErr;  { save error codes to report trouble }
  241.       logEOF : longint;  { length of file }
  242.       theBufHndl : Handle;  { to allocate memory for reading in contents of file }
  243.       numStr : Str255;
  244.  
  245.     PROCEDURE passReturnValue (theMsg : Str255); { set theResult }
  246.     BEGIN
  247.       paramPtr^.returnValue := PasToZero(paramPtr, theMsg);
  248.     END;
  249.  
  250.   BEGIN
  251.     IF paramPtr^.paramCount <> 0 THEN
  252. { if we got parameters then return version number }
  253.       passReturnValue('FileToClip XCMD 1.5, 15 March 1989, ┬⌐1988-1989 Dartmouth College')
  254.     ELSE
  255.       BEGIN
  256. { select text file to read using SFGetFile }
  257.  
  258.         dlgt := DialogTHndl(GetResource('DLOG',getDlgID));
  259.         if dlgt <> nil then
  260.           begin
  261.           r := dlgt^^.boundsRect;
  262.           screen := GetScreenBitsBounds;
  263.           h := ((screen.right - screen.left) - (r.right - r.left)) div 2;
  264.           v := ((screen.bottom - screen.top) - (r.bottom - r.top)) div 2;
  265.           SetPt(where, h, v);
  266.           end
  267.         else SetPt(where, 82, 75);             { tell SFGetFile where to put the dialog box }
  268.         typeList[0] := 'TEXT';                { tell SFGetFile to display only text files }
  269.         SFGetFile(where, '', NIL, 1, typeList, NIL, reply);          { call SFGetFile }
  270.  
  271.         IF reply.good = TRUE THEN          { continue only if user actually selected a file }
  272.           BEGIN
  273.             WITH reply DO
  274.               err := FSOpen(fName, vRefNum, theRefNum);  { open the file }
  275.  
  276.             IF err = noErr THEN              { continue only if file could be opened }
  277.               BEGIN
  278.                 err := GetEOF(theRefNum, logEOF);
  279.  
  280.                 IF err = noErr THEN
  281.                   BEGIN   { set up the buffer in memory for reading in logEOF characters }
  282.                     theBufHndl := NewHandle(logEOF);
  283.                     err := MemError;  { save the result in case we want to report an error }
  284.                     IF (theBufHndl <> NIL) AND (err = noErr) THEN
  285.                            { continue only if enough memory is available }
  286.                       BEGIN
  287.                         MoveHHi(theBufHndl);
  288.                         HLock(theBufHndl);  { lock down our buffer }
  289.                 { read logEOF characters into the location pointed to by theBufHandle^ }
  290.                         err := FSRead(theRefNum, logEOF, theBufHndl^);
  291.  
  292.                         IF err = noErr THEN      { continue only if the read worked }
  293.                           BEGIN
  294.                             err := ZeroScrap;  { reinitialize the scrap }
  295.  
  296.                             IF err = noErr THEN  { continue only if we reinitialized the scrap }
  297.                               BEGIN
  298.                                 err := PutScrap(logEOF, 'TEXT', theBufHndl^);
  299.                                                   { put our text onto the scrap }
  300.                                 IF err = noErr THEN
  301.                                   err := TEFromScrap;  { make our text available to TextEdit }
  302.  
  303.                               END;   { if err = noErr when clearing the scrap }
  304.                           END;  { if err = noErr when reading the file into memory }
  305.                         DisposHandle(theBufHndl);   { deallocate the memory we used }
  306.                       END;  { if theBufHndl <> nil and MemError = noErr }
  307.                   END;  { if there was no problem getting the EOF }
  308.                 err := FSClose(theRefNum);            { close the file }
  309.               END;   { if err = noErr in opening the file }
  310.             IF err = noErr THEN
  311.               PassReturnValue(BuildThePathName(reply.fName, reply.vRefNum))
  312.             ELSE
  313.               BEGIN
  314.               NumToStr(paramPtr, err, numStr);
  315.               PassReturnValue(CONCAT('Error ', numStr));
  316.               END;
  317.           END;  { if the SF reply.good = TRUE }
  318.       END;
  319.   END;
  320.  
  321. END.